<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: FileIO</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">FileIO</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../files/lib/svengali/file_io_rb.html">
                lib/svengali/file_io.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000073">get_contents_remote</a>&nbsp;&nbsp;
      <a href="#M000072">is_exist</a>&nbsp;&nbsp;
      <a href="#M000067">pull_a_file</a>&nbsp;&nbsp;
      <a href="#M000071">pull_dir</a>&nbsp;&nbsp;
      <a href="#M000069">pull_files</a>&nbsp;&nbsp;
      <a href="#M000066">push_a_file</a>&nbsp;&nbsp;
      <a href="#M000070">push_dir</a>&nbsp;&nbsp;
      <a href="#M000068">push_files</a>&nbsp;&nbsp;
      <a href="#M000074">write_contents_remote</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">


    <div id="constants-list">
      <h3 class="section-bar">Constants</h3>

      <div class="name-list">
        <table summary="Constants">
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">BUFFER_SIZE</td>
          <td>=</td>
          <td class="context-item-value">1024</td>
        </tr>
        </table>
      </div>
    </div>



      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000073" class="method-detail">
        <a name="M000073"></a>

        <div class="method-heading">
          <a href="#M000073" class="method-signature">
          <span class="method-name">get_contents_remote</span><span class="method-args">(remote_filepath_str)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000073-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000073-source">
<pre>
<span class="ruby-comment cmt"># File lib/svengali/file_io.rb, line 84</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">get_contents_remote</span>(<span class="ruby-identifier">remote_filepath_str</span>)
    <span class="ruby-identifier">debug_p</span>(<span class="ruby-node">&quot;get_contents_remote from #{remote_filepath_str}&quot;</span>)
    <span class="ruby-identifier">handle</span> = <span class="ruby-ivar">@sftp_session</span>.<span class="ruby-identifier">open!</span>(<span class="ruby-identifier">remote_filepath_str</span>)
    <span class="ruby-identifier">contents</span> = <span class="ruby-constant">String</span>.<span class="ruby-identifier">new</span>()
    <span class="ruby-identifier">offset</span> = <span class="ruby-value">0</span>
    <span class="ruby-keyword kw">while</span> (<span class="ruby-identifier">data</span> = <span class="ruby-ivar">@sftp_session</span>.<span class="ruby-identifier">read!</span>(<span class="ruby-identifier">handle</span>, <span class="ruby-identifier">offset</span>, <span class="ruby-constant">BUFFER_SIZE</span>)) <span class="ruby-operator">!=</span> <span class="ruby-keyword kw">nil</span>
      <span class="ruby-identifier">contents</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">data</span>
      <span class="ruby-identifier">offset</span> <span class="ruby-operator">+=</span> <span class="ruby-constant">BUFFER_SIZE</span>
    <span class="ruby-keyword kw">end</span>
    <span class="ruby-ivar">@sftp_session</span>.<span class="ruby-identifier">close!</span>(<span class="ruby-identifier">handle</span>)

    <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">contents</span>
  <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000072" class="method-detail">
        <a name="M000072"></a>

        <div class="method-heading">
          <a href="#M000072" class="method-signature">
          <span class="method-name">is_exist</span><span class="method-args">(remote_path)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
return -&gt; boolean :
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000072-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000072-source">
<pre>
<span class="ruby-comment cmt"># File lib/svengali/file_io.rb, line 72</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">is_exist</span>(<span class="ruby-identifier">remote_path</span>)
    <span class="ruby-keyword kw">begin</span>
      <span class="ruby-ivar">@sftp_session</span>.<span class="ruby-identifier">stat!</span>(<span class="ruby-identifier">remote_path</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">response</span><span class="ruby-operator">|</span>
        <span class="ruby-comment cmt">#returns whether exists or not</span>
        <span class="ruby-keyword kw">next</span> <span class="ruby-identifier">response</span>.<span class="ruby-identifier">ok?</span>
      }
    <span class="ruby-keyword kw">rescue</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
      <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">false</span>
    <span class="ruby-keyword kw">end</span>
  <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000067" class="method-detail">
        <a name="M000067"></a>

        <div class="method-heading">
          <a href="#M000067" class="method-signature">
          <span class="method-name">pull_a_file</span><span class="method-args">(remote_path,local_path)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000067-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000067-source">
<pre>
<span class="ruby-comment cmt"># File lib/svengali/file_io.rb, line 14</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">pull_a_file</span>(<span class="ruby-identifier">remote_path</span>,<span class="ruby-identifier">local_path</span>)
    <span class="ruby-identifier">debug_p</span>(<span class="ruby-node">&quot;pull_a_file from #{remote_path} to #{local_path}&quot;</span>)

    <span class="ruby-keyword kw">begin</span>
      <span class="ruby-ivar">@sftp_session</span>.<span class="ruby-identifier">download!</span>(<span class="ruby-identifier">remote_path</span>,<span class="ruby-identifier">local_path</span>)
    <span class="ruby-keyword kw">rescue</span>
      <span class="ruby-identifier">debug_p</span> <span class="ruby-identifier">remote_path</span> <span class="ruby-operator">+</span> <span class="ruby-value str">&quot; is not found on remote!!&quot;</span>
      <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span>
    <span class="ruby-keyword kw">end</span>
  <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000071" class="method-detail">
        <a name="M000071"></a>

        <div class="method-heading">
          <a href="#M000071" class="method-signature">
          <span class="method-name">pull_dir</span><span class="method-args">(remote_path,local_path)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000071-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000071-source">
<pre>
<span class="ruby-comment cmt"># File lib/svengali/file_io.rb, line 61</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">pull_dir</span>(<span class="ruby-identifier">remote_path</span>,<span class="ruby-identifier">local_path</span>)
    <span class="ruby-identifier">debug_p</span>(<span class="ruby-node">&quot;pull_dir from #{remote_path} to #{local_path}&quot;</span>)
    <span class="ruby-keyword kw">begin</span>
      <span class="ruby-ivar">@sftp_session</span>.<span class="ruby-identifier">download!</span>(<span class="ruby-identifier">remote_path</span>,<span class="ruby-identifier">local_path</span>,<span class="ruby-identifier">:recursive</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span>)
    <span class="ruby-keyword kw">rescue</span>
      <span class="ruby-identifier">debug_p</span> <span class="ruby-identifier">remote_path</span> <span class="ruby-operator">+</span> <span class="ruby-value str">&quot; is not found on remote!!&quot;</span>
      <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span>
    <span class="ruby-keyword kw">end</span>
  <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000069" class="method-detail">
        <a name="M000069"></a>

        <div class="method-heading">
          <a href="#M000069" class="method-signature">
          <span class="method-name">pull_files</span><span class="method-args">(remote_path,local_path)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
pull files on specified dir. only one level.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000069-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000069-source">
<pre>
<span class="ruby-comment cmt"># File lib/svengali/file_io.rb, line 38</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">pull_files</span>(<span class="ruby-identifier">remote_path</span>,<span class="ruby-identifier">local_path</span>)
    <span class="ruby-identifier">debug_p</span>(<span class="ruby-node">&quot;pull_files from #{remote_path} to #{local_path}&quot;</span>)
    <span class="ruby-ivar">@sftp_session</span>.<span class="ruby-identifier">dir</span>.<span class="ruby-identifier">foreach</span>(<span class="ruby-identifier">remote_path</span>){ <span class="ruby-operator">|</span><span class="ruby-identifier">path</span><span class="ruby-operator">|</span>
      <span class="ruby-comment cmt">#unless path.name == &quot;.&quot; || path.name == &quot;..&quot;</span>
      <span class="ruby-comment cmt">#not directory</span>
      <span class="ruby-keyword kw">unless</span> <span class="ruby-regexp re">/^d/</span> <span class="ruby-operator">=~</span> <span class="ruby-identifier">path</span>.<span class="ruby-identifier">longname</span>
          <span class="ruby-ivar">@sftp_session</span>.<span class="ruby-identifier">download!</span>(<span class="ruby-identifier">remote_path</span> <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;/&quot;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">path</span>.<span class="ruby-identifier">name</span>,<span class="ruby-identifier">local_path</span> <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;/&quot;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">path</span>.<span class="ruby-identifier">name</span>)
      <span class="ruby-keyword kw">end</span>
      <span class="ruby-comment cmt">#end</span>
    }
  <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000066" class="method-detail">
        <a name="M000066"></a>

        <div class="method-heading">
          <a href="#M000066" class="method-signature">
          <span class="method-name">push_a_file</span><span class="method-args">(local_path,remote_path)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000066-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000066-source">
<pre>
<span class="ruby-comment cmt"># File lib/svengali/file_io.rb, line 9</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">push_a_file</span>(<span class="ruby-identifier">local_path</span>,<span class="ruby-identifier">remote_path</span>)
    <span class="ruby-identifier">debug_p</span>(<span class="ruby-node">&quot;push_a_file from #{local_path} to #{remote_path}&quot;</span>)
    <span class="ruby-ivar">@sftp_session</span>.<span class="ruby-identifier">upload!</span>(<span class="ruby-identifier">local_path</span>,<span class="ruby-identifier">remote_path</span>)
  <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000070" class="method-detail">
        <a name="M000070"></a>

        <div class="method-heading">
          <a href="#M000070" class="method-signature">
          <span class="method-name">push_dir</span><span class="method-args">(local_path,remote_path)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Note: if there is a directory which has same name with <b>local_path</b>,
remote_directory is removed before coping directory
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000070-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000070-source">
<pre>
<span class="ruby-comment cmt"># File lib/svengali/file_io.rb, line 51</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">push_dir</span>(<span class="ruby-identifier">local_path</span>,<span class="ruby-identifier">remote_path</span>)
    <span class="ruby-identifier">debug_p</span>(<span class="ruby-node">&quot;push_dir from #{local_path} to #{remote_path}&quot;</span>)
    <span class="ruby-keyword kw">begin</span>
      <span class="ruby-ivar">@sftp_session</span>.<span class="ruby-identifier">upload!</span>(<span class="ruby-identifier">local_path</span>,<span class="ruby-identifier">remote_path</span>)
    <span class="ruby-keyword kw">rescue</span>
      <span class="ruby-ivar">@ssh</span>.<span class="ruby-identifier">exec!</span>(<span class="ruby-node">&quot;rm -rf #{remote_path}&quot;</span>)
      <span class="ruby-keyword kw">retry</span>
    <span class="ruby-keyword kw">end</span>
  <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000068" class="method-detail">
        <a name="M000068"></a>

        <div class="method-heading">
          <a href="#M000068" class="method-signature">
          <span class="method-name">push_files</span><span class="method-args">(local_path,remote_path)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
push files on specified dir. only one level. directories are not copied.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000068-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000068-source">
<pre>
<span class="ruby-comment cmt"># File lib/svengali/file_io.rb, line 27</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">push_files</span>(<span class="ruby-identifier">local_path</span>,<span class="ruby-identifier">remote_path</span>)
    <span class="ruby-identifier">debug_p</span>(<span class="ruby-node">&quot;push_files from #{local_path} to #{remote_path}&quot;</span>)
    <span class="ruby-identifier">local_dir</span> = <span class="ruby-constant">Dir</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">local_path</span>)
    <span class="ruby-identifier">local_dir</span>.<span class="ruby-identifier">each</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">path</span><span class="ruby-operator">|</span>
      <span class="ruby-keyword kw">unless</span> <span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-identifier">ftype</span>(<span class="ruby-identifier">local_path</span> <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;/&quot;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">path</span>) <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;directory&quot;</span>
        <span class="ruby-ivar">@sftp_session</span>.<span class="ruby-identifier">upload!</span>(<span class="ruby-identifier">local_path</span> <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;/&quot;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">path</span>,<span class="ruby-identifier">remote_path</span> <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;/&quot;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">path</span>)
      <span class="ruby-keyword kw">end</span>
    }
  <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000074" class="method-detail">
        <a name="M000074"></a>

        <div class="method-heading">
          <a href="#M000074" class="method-signature">
          <span class="method-name">write_contents_remote</span><span class="method-args">(remote_filepath_str,contents_str)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
over writes contens from the start
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000074-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000074-source">
<pre>
<span class="ruby-comment cmt"># File lib/svengali/file_io.rb, line 99</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">write_contents_remote</span>(<span class="ruby-identifier">remote_filepath_str</span>,<span class="ruby-identifier">contents_str</span>)
    <span class="ruby-identifier">debug_p</span>(<span class="ruby-node">&quot;write_contents_remote to #{remote_filepath_str}&quot;</span>)
    <span class="ruby-identifier">handle</span> = <span class="ruby-ivar">@sftp_session</span>.<span class="ruby-identifier">open!</span>(<span class="ruby-identifier">remote_filepath_str</span>,<span class="ruby-value str">&quot;w&quot;</span>)
    <span class="ruby-ivar">@sftp_session</span>.<span class="ruby-identifier">write!</span>(<span class="ruby-identifier">handle</span>, <span class="ruby-value">0</span>, <span class="ruby-identifier">contents_str</span>)
    <span class="ruby-ivar">@sftp_session</span>.<span class="ruby-identifier">close!</span>(<span class="ruby-identifier">handle</span>)
  <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>